package com.chat.common.chat.service.adapter;

import com.chat.common.chat.domain.entity.RoomGroup;
import com.chat.common.chat.domain.enums.HotFlagEnum;
import com.chat.common.chat.domain.enums.RoomTypeEnum;
import com.chat.common.chat.domain.entity.Room;
import com.chat.common.chat.domain.entity.RoomFriend;
import com.chat.common.user.domain.entity.User;
import com.chat.common.user.domain.enums.NormalOrNoEnum;

import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

public class ChatAdapter {
    public static final String SEPARATOR = ",";
    public static String generateRoomKey(List<Long> uidList) {
        return uidList.stream().sorted()
                .map(String::valueOf)
                .collect(Collectors.joining(SEPARATOR));
    }

    public static Room buildRoom(RoomTypeEnum roomTypeEnum) {
        return Room.builder()
                .type(roomTypeEnum.getType())
                .hotFlag(HotFlagEnum.NOT.getType())
                .build();
    }

    public static RoomFriend buildFriendRoom(Long roomId, List<Long> uidList) {
        List<Long> collect = uidList.stream().sorted().collect(Collectors.toList());
        return RoomFriend.builder()
                .roomId(roomId)
                .uid1(collect.get(0))
                .uid2(collect.get(1))
                .roomKey(generateRoomKey(uidList))
                .status(NormalOrNoEnum.NORMAL.getStatus())
                .build();
    }

    public static Set<Long> getFriendUidSet(Collection<RoomFriend> values, Long uid) {
        return values.stream()
                .map(a -> getFriendUid(a, uid))
                .collect(Collectors.toSet());
    }


    /**
     * 获取好友uid
     */
    public static Long getFriendUid(RoomFriend roomFriend, Long uid) {
        return Objects.equals(uid, roomFriend.getUid1()) ? roomFriend.getUid2() : roomFriend.getUid1();
    }

    public static RoomGroup buildGroupRoom(User userInfo, Long roomId) {
        return RoomGroup.builder()
                .name(userInfo.getName() + "的群聊")
                .avatar(userInfo.getAvatar())
                .roomId(roomId)
                .build();
    }
}
